package com.fjec.gateway.shiro;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 账号密码登录验证器
 */
@Slf4j
public class UserRealm extends AuthorizingRealm {


    /**
     * 鉴权
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String openId = principals.getPrimaryPrincipal().toString();
        SimpleAuthorizationInfo account = new SimpleAuthorizationInfo();
        return account;
    }

    /**
     * 认证
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
        String openId = upToken.getUsername();
        String password = new String(upToken.getPassword());
        log.debug("authentication username=[" + openId + "] password=[" + password + "]");


        return null;
    }

}
